"""
Case Type   : datakit录制回放工具
Case Name   : 创建录制回放任务时新增数据源
Create At   : 2025/3/26
Owner       : @lonely-dance
Description :
    1.新增source端数据源
    2.新增target端数据源
    3.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.datakit_api.DataMigration import DataMigrationApi
from testcase.utils.CommonDatakit import CommonDatakit


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.mysql_node = Node('mysql5')
        self.primary_node = Node('PrimaryDbUser')
        self.com_sh = CommonSH('PrimaryDbUser')
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.datakit_sh = CommonDatakit(self.token)
        self.data_migration_api = DataMigrationApi(self.token)
        self.data1_source = {"username": f"{self.mysql_node.db_user}",
                             "password": f"{self.mysql_node.db_password}",
                             "url": f"jdbc:mysql://{self.mysql_node.ssh_host}:{self.mysql_node.db_port}"}
        self.data2_source = {"clusterName": f"{self.mysql_node.ssh_host}({self.mysql_node.db_port})-1",
                             "dbType": "MYSQL", "deployType": "SINGLE_NODE",
                             "nodes": [{"id": "", "tabName": 1,
                                        "url": f"jdbc:mysql://{self.mysql_node.ssh_host}:{self.mysql_node.db_port}",
                                        "urlSuffix": "", "ip": f"{self.mysql_node.ssh_host}", "port": self.mysql_node.db_port,
                                        "username": f"{self.mysql_node.db_user}",
                                        "password": f"{self.mysql_node.db_password}",
                                        "props": [{"name": "", "value": ""}], "status": 1,
                                        "extendProps": "[{\"name\":\"\",\"value\":\"\"}]"}]}
        self.data1_target = {
            "username": f"{self.primary_node.db_user}", "password": f"{self.primary_node.db_password}",
            "url": f"jdbc:opengauss://{self.primary_node.ssh_host}:{self.primary_node.db_port}/postgres"}
        self.data2_target = {"clusterName": f"{self.primary_node.ssh_host}({self.primary_node.db_port})-1",
                             "dbType": "OPENGAUSS",
                             "deployType": "SINGLE_NODE",
                             "nodes": [{"id": "", "tabName": 1,
                                        "url": f"jdbc:opengauss://{self.primary_node.ssh_host}:{self.primary_node.db_port}/postgres",
                                        "urlSuffix": "", "ip": f"{self.primary_node.ssh_host}",
                                        "port": self.primary_node.db_port,
                                        "username": f"{self.primary_node.db_user}",
                                        "password": f"{self.primary_node.db_password}",
                                        "props": [{"name": "", "value": ""}], "status": 1,
                                        "extendProps": "[{\"name\":\"\",\"value\":\"\"}]"}]}

    def test_transcribe_replay(self):
        text = '----step1:添加source端数据源----'
        self.log.info(text)
        text1 = '----联通性测试----'
        self.log.info(text1)
        response = self.com_api.post_jdbcDbClusterNode_ping(self.data1_source)
        self.log.info(response.json())
        self.assertEqual(200, response.json()["code"], '执行失败' + text1)

        text2 = '----添加数据源----'
        self.log.info(text2)
        response = self.com_api.post_jdbcDbCluster_add(self.data2_source)
        self.log.info(response.json())
        self.assertEqual(200, response.json()["code"], '执行失败' + text2)

        text3 = '---查询数据源是否添加成功'
        self.log.info(text3)
        response = self.data_migration_api.get_data_migration_resource('sourceClusters')
        self.log.info(response.json())
        self.assertIn(self.data2_source['clusterName'], response.text, '执行失败' + text3)

        text = '----step2:添加source端数据源----'
        self.log.info(text)
        text4 = '----联通性测试----'
        self.log.info(text4)
        response = self.com_api.post_jdbcDbClusterNode_ping(self.data1_target)
        self.log.info(response.json())
        self.assertEqual(200, response.json()["code"], '执行失败' + text4)

        text5 = '----添加数据源----'
        self.log.info(text5)
        response = self.com_api.post_jdbcDbCluster_add(self.data2_target)
        self.log.info(response.json())
        self.assertEqual(200, response.json()["code"], '执行失败' + text5)

        text6 = '----查询数据源是否添加成功----'
        self.log.info(text6)
        response = self.data_migration_api.get_data_migration_resource('targetClusters')
        self.log.info(response.json())
        self.assertIn(self.data2_target['clusterName'], response.text, '执行失败' + text6)

    def tearDown(self):
        text = '----step3:清理环境----'
        self.log.info(text)
        self.log.info('----获取clusterId,删除实例----')
        response1 = self.com_api.get_jdbcDbCluster_page(ip=self.mysql_node.ssh_host)
        self.log.info(response1.json())
        self.assertIsNotNone(response1.json()['rows'], '执行失败' + text)
        source_cluster_node_id = response1.json()['rows'][0]['nodes'][0]['clusterNodeId']
        self.log.info(source_cluster_node_id)
        response2 = self.com_api.delete_jdbcDbCluster(source_cluster_node_id)
        self.log.info(response2.json())
        self.assertEqual(200, response2.json()['code'], '执行失败' + text)

        response3 = self.com_api.get_jdbcDbCluster_page(ip=self.primary_node.ssh_host)
        self.log.info(response3.json())
        self.assertIsNotNone(response3.json()['rows'], '执行失败' + text)
        target_cluster_node_id = response3.json()['rows'][0]['nodes'][0]['clusterNodeId']
        self.log.info(target_cluster_node_id)
        response4 = self.com_api.delete_jdbcDbCluster(target_cluster_node_id)
        self.log.info(response4.json())
        self.assertEqual(200, response4.json()['code'], '执行失败' + text)

        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
